<?php
require_once "core/db.abstract.php";

class PgsqlDb extends Db_Abstract
{
    public function PgsqlDb ($connstr) {
        $this->connstr = $connstr;
        Db_Abstract::Db_Abstract();
    }

    public function connect () {
        $this->res = pg_connect($this->connstr);
        return $this->checkConnection();
    }

    public function close () {
        if ($this->checkConnection()) {
            pg_close($this->res);
            $this->res = NULL;
        }
        return true;
    }

    public function selectDb ($db_name) {
        throw new DbException("pgsql is not suppoted select db.");
    }

    public function fetchSql ($sql) {

        if (!$this->checkConnection()) {
            throw new DbException("invalid resources");
        }
        $result = array();
        $obj = pg_query($sql);
        if ($obj === False) {
            return $result;
        }
        while ( $row = pg_fetch_array($obj) ) {
            $tmp_row = array();
            foreach ($row as $k=>$v) {
                if ( !is_integer($k) ) {
                    $tmp_row[$k] = $v;
                }
            }
            array_push($result, $tmp_row);
        }
        return $result;
    }

    public function execSql ($sql) {

        if (!$this->checkConnection()) {
            throw new DbException("invalid resources");
        }
        try {
            pg_query($sql);
        } catch(Exception $_ex) {
            return false;
        }
        return true;
    }


    public function execProc ($procName, $params, $fetch=False) {
        throw new DbException("pgsql is not supported stored procedures.");
    }

    public function _ ($s) {
        return str_replace("'", "''", $s);
    }
    public function __ ($s) {
        return "'" . PgsqlDb::_($s) . "'";
    }


}





